package com.internal.Internalhibernate.server.dao;

// Generated Feb 24, 2012 1:32:12 PM by Hibernate Tools 3.3.0.GA

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import com.internal.Internalhibernate.client.SecUsers;
import com.internal.Internalhibernate.server.SessionFactoryUtil;

/**
 * Home object for domain model class SecUsers.
 * 
 * @see com.internal.Internalhibernate.client.SecUsers
 * @author Hibernate Tools
 */
public class SecUsersDAO {

	private static final Log log = LogFactory.getLog(SecUsersDAO.class);

	private final SessionFactory sessionFactory = SessionFactoryUtil
			.getSessionFactory();

	public void persist(SecUsers transientInstance) {
		log.debug("persisting SecUsers instance");
		Transaction trans = null;
		try {
			Session session = sessionFactory.getCurrentSession();
			trans = session.beginTransaction();
			session.persist(transientInstance);
			trans.commit();
			log.debug("persist successful");
		} catch (RuntimeException re) {
			trans.rollback();
			log.error("persist failed", re);
			throw re;
		}
	}

	public void attachDirty(SecUsers instance) {
		log.debug("attaching dirty SecUsers instance");
		Transaction trans = null;
		try {
			Session session = sessionFactory.getCurrentSession();
			trans = session.beginTransaction();
			session.saveOrUpdate(instance);
			trans.commit();
			log.debug("attach successful");
		} catch (RuntimeException re) {
			trans.rollback();
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(SecUsers instance) {
		log.debug("attaching clean SecUsers instance");
		Transaction trans = null;
		try {
			Session session = sessionFactory.getCurrentSession();
			trans = session.beginTransaction();
			session.lock(instance, LockMode.NONE);
			trans.commit();
			log.debug("attach successful");
		} catch (RuntimeException re) {
			trans.rollback();
			log.error("attach failed", re);
			throw re;
		}
	}

	public void delete(SecUsers persistentInstance) {
		log.debug("deleting SecUsers instance");
		Transaction trans = null;
		try {
			Session session = sessionFactory.getCurrentSession();
			trans = session.beginTransaction();
			session.delete(persistentInstance);
			trans.commit();
			log.debug("delete successful");
		} catch (RuntimeException re) {
			trans.rollback();
			log.error("delete failed", re);
			throw re;
		}
	}

	public SecUsers merge(SecUsers detachedInstance) {
		log.debug("merging SecUsers instance");
		Transaction trans = null;
		try {
			Session session = sessionFactory.getCurrentSession();
			trans = session.beginTransaction();
			SecUsers result = (SecUsers) session.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			trans.rollback();
			log.error("merge failed", re);
			throw re;
		}
	}

	public SecUsers findById(String id) {
		log.debug("getting SecUsers instance with id: " + id);
		try {
			Session session = sessionFactory.getCurrentSession();
			session.beginTransaction();
			SecUsers instance = (SecUsers) session.get("com.internal.Internalhibernate.client.SecUsers", id);
			if (instance == null) {
				log.debug("get successful, no instance found");
			} else {
				log.debug("get successful, instance found");
			}
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	public SecUsers getUserByAuthInfo(String userId, String pw) {
		try {
			Session session = sessionFactory.getCurrentSession();
		    session.beginTransaction();
			Criteria crit = session.createCriteria(SecUsers.class);
			Criterion idCond = Restrictions.eq("userId", userId);
			Criterion pwCond = Restrictions.eq("userPassword", pw);
			Criterion cond = Restrictions.and(idCond, pwCond);
			crit.add(cond);
			return (SecUsers) crit.uniqueResult();
		} catch (RuntimeException re) {
			log.error("get user by authentication info failed :", re);
			throw re;
		}
	}
	
	public List<SecUsers> findAll() {
		Session session = sessionFactory.getCurrentSession();
		session.beginTransaction();
		Query q = session.createQuery("from SecUsers");
		return q.list();
	}
	
	public SecUsers isEmailExist(String id) {
		try {
			Session session = sessionFactory.getCurrentSession();
			session.beginTransaction();
			Query q = session.createQuery("from SecUsers where userId = :userId");
			q.setParameter("userId", id);
			return (SecUsers) q.uniqueResult();
		} catch (RuntimeException re) {
			log.error("get user by authentication info failed :", re);
			throw re;
		}
	}
}
